package com.jiuhao.conf;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RequestOptions;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author baidu-106
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class EsConnectionChecker implements CommandLineRunner {

    private final RestHighLevelClient restHighLevelClient;

    @Override
    public void run(String... args) {
        try {
            // 执行ping检测（超时时间可根据需要调整）
            boolean isConnected = restHighLevelClient.ping(RequestOptions.DEFAULT);
            if (isConnected) {
                log.info("✅ Elasticsearch连接检测成功，服务正常");
            } else {
                log.error("❌ Elasticsearch连接检测失败，请检查服务状态");
            }
        } catch (IOException e) {
            log.error("❗ Elasticsearch连接检测异常: {}", e.getMessage(), e);
        }
    }
}
